package com.heima.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.Result;
import com.heima.domain.dto.NodeDto;
import com.heima.domain.po.Node;
import com.heima.domain.po.VendingMachine;
import com.heima.domain.po.VmType;
import com.heima.domain.vo.NodePageVo;
import com.heima.domain.vo.NodeVo;
import com.heima.domain.vo.RegionVo;
import com.heima.domain.vo.VmTypeVo;
import com.heima.mapper.NodeMapper;
import com.heima.mapper.RegionMapper;
import com.heima.mapper.VendingMachineMapper;
import com.heima.mapper.VmTypeMapper;
import com.heima.service.NodeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class NodeServiceImpl extends ServiceImpl<NodeMapper, Node> implements NodeService {

    @Autowired
    private NodeMapper nodeMapper;

    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Autowired
    private VmTypeMapper vmTypeMapper;

    @Autowired
    private RegionMapper regionMapper;

    /**
     * 点位搜索
     *
     * @param pageIndex
     * @param pageSize
     * @param name
     * @param regionId
     * @return
     */
    @Override
    public Result getNodeByPage(Integer pageIndex, Integer pageSize, String name, Long regionId) {
        Page<NodePageVo> page = new Page<>(pageIndex,pageSize);
        Page<NodePageVo> page1 = nodeMapper.getNodeByPage(page,name,regionId,null);
        page1.getRecords().stream().forEach(nodePageVo -> {
            Page<RegionVo> page2 = new Page<>(1,1);
            Page<RegionVo> byPage = regionMapper.getByPage(page2, null, nodePageVo.getRegionId());
            nodePageVo.setRegion(byPage.getRecords().get(0));
        });
        Result of = Result.of(page1);
        return of;
    }

    /**
     * 新增点位
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean addNode(NodeDto dto) {
        Node node = new Node();
        BeanUtils.copyProperties(dto,node);
        node.setCreateTime(LocalDateTime.now());
        node.setUpdateTime(LocalDateTime.now());
        try {
            save(node);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 修改点位
     *
     * @param nodeId
     * @param dto
     * @return
     */
    @Override
    public Boolean updateNode(Long nodeId, NodeDto dto) {
        Node node = new Node();
        BeanUtils.copyProperties(dto,node);
        node.setUpdateTime(LocalDateTime.now());
        try {
            lambdaUpdate().eq(nodeId != null,Node::getId,nodeId).update(node);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 删除点位
     *
     * @param nodeId
     * @return
     */
    @Override
    public Boolean deleteNode(Long nodeId) {
        try {
            lambdaUpdate().eq(nodeId != null,Node::getId,nodeId).remove();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 获取点位详情
     *
     * @param nodeId
     * @return
     */
    @Override
    public List<NodeVo> getByNodeId(Long nodeId) {
        List<NodeVo> list = new ArrayList<>();
        QueryWrapper<VendingMachine> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(nodeId != null, VendingMachine::getNodeId,nodeId);
        List<VendingMachine> vendingMachines = vendingMachineMapper.selectList(wrapper);
        for (VendingMachine vendingMachine : vendingMachines) {
            NodeVo vo = new NodeVo();
            BeanUtils.copyProperties(vendingMachine,vo);

            QueryWrapper<VmType> wrapperVmType = new QueryWrapper<>();
            wrapperVmType.lambda().eq(ObjectUtil.isNotEmpty(vo.getVmType()), VmType::getTypeId, vo.getVmType());
            VmType vmType = vmTypeMapper.selectOne(wrapperVmType);
            VmTypeVo vmTypeVo = new VmTypeVo();
            BeanUtils.copyProperties(vmType,vmTypeVo);
            vo.setType(vmTypeVo);

            Node one = null;
            try {
                one = this.lambdaQuery().eq(nodeId != null, Node::getId, nodeId).one();
            } catch (Exception e) {
                e.printStackTrace();
            }
            NodePageVo nodePageVo = new NodePageVo();
            BeanUtils.copyProperties(one,nodePageVo);
            Page<RegionVo> page2 = new Page<>(1,1);
            Page<RegionVo> byPage = regionMapper.getByPage(page2, null, nodePageVo.getRegionId());
            nodePageVo.setRegion(byPage.getRecords().get(0));

            vo.setNode(nodePageVo);
            vo.setRegion(nodePageVo.getRegion());
            list.add(vo);
        }


        return list;
    }
}
